package cn.joyconn.cms.views.directive.cms;

// Generated 2015-5-10 17:54:56 by com.publiccms.common.source.SourceGenerator

import cn.joyconn.cms.common.base.AbstractTemplateDirective;
import cn.joyconn.cms.entities.cms.CmsContent;
import cn.joyconn.cms.entities.cms.CmsTag;
import cn.joyconn.cms.entities.log.LogOperate;
import cn.joyconn.cms.logic.service.cms.CmsContentService;
import cn.joyconn.cms.logic.service.cms.CmsTagService;
import cn.joyconn.cms.logic.service.log.LogOperateService;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.joyconn.cms.common.handler.PageHandler;
import com.joyconn.cms.common.handler.RenderHandler;
import org.hibernate.search.util.logging.impl.LoggerHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

import static com.joyconn.cms.common.tools.CommonUtils.empty;
import static com.joyconn.cms.common.tools.CommonUtils.getMinuteDate;

/**
 *
 * CmsContentListDirective
 * 
 */
@Component
    public class CmsContentListByUpdateTimeDirective extends AbstractTemplateDirective {

    @Override
    public void execute(RenderHandler handler) throws IOException, Exception {

//        Date startDate = handler.getDate("startDate");
//        Date endDate = handler.getDate("endDate");
//        PageHandler page = logOperateService.getPage(getSite(handler).getId(), null, null,
//                null, startDate, endDate,
//                null, null, "desc",
//                1,Integer.MAX_VALUE);
        PageHandler page = logOperateService.getPage(getSite(handler).getId(), "", "",
                null, handler.getDate("startDate"), handler.getDate("endDate"),
                "", null, "",
                1, Integer.MAX_VALUE);
        Map<Long,Map<String,String>> cmsContentMap = new HashMap<>();
        if(page.getTotalCount()>0){
            List<?> logOperates = page.getList();
            LogOperate logOperate;
            CmsContent cmsContent;
            CmsTag tagEntity;

            Map<Long,CmsTag> tagMap = new HashMap<>();
            Map<String,String> cmsContentObjectMap=new HashMap<>();
            String tags[]=null;
            List<String> tagName=Lists.newArrayList();
            Long tagID=0L;
            for(Object logOperatObject:logOperates){
                try {
                    if (logOperatObject != null) {
                        logOperate = (LogOperate) logOperatObject;
                        if (logOperate != null) {
                            if (logOperate.getOperate().equals("update.content") || logOperate.getOperate().equals("save.content")) {
                                cmsContent = objectMapper.readValue(logOperate.getContent(), CmsContent.class);
                                cmsContent = service.getEntity(cmsContent.getId());
                                if (cmsContent != null) {
                                    if(!cmsContentMap.containsKey(cmsContent.getId())) {
                                        tagName.clear();
                                        if(cmsContent.getTagIds()!=null){
                                            tags  =cmsContent.getTagIds().split(" ");
                                            if(tags!=null){
                                                for(String tag:tags){
                                                    if(Strings.isNullOrEmpty(tag)){
                                                        continue;
                                                    }
                                                    tagID=Long.valueOf(tag);
                                                    if(!tagMap.containsKey(tagID)){
                                                        tagEntity = tagService.getEntity(tagID);
                                                        if(tagEntity!=null){
                                                            tagMap.put(tagEntity.getId(),tagEntity);
                                                            tagName.add(tagEntity.getName());
                                                        }
                                                    }else{
                                                        tagName.add(tagMap.get(tagID).getName());
                                                    }
                                                }
                                            }
                                        }
                                        cmsContentObjectMap=new HashMap<>();
                                        cmsContentObjectMap.put("content",objectMapper.writeValueAsString(cmsContent));
                                        cmsContentObjectMap.put("tag",objectMapper.writeValueAsString(tagName));
                                        cmsContentMap.put(cmsContent.getId(), cmsContentObjectMap);
                                    }

                                }
                            }
                        }
                    }
                }catch (Exception ex){
                    String sss = ex.getMessage();
                }
            }
        }
        page.setList(Lists.newArrayList(cmsContentMap.values()));
        page.setPageIndex(1);
        page.setPageSize(page.getList().size());
        page.setTotalCount(page.getPageSize());
        handler.put("page", page).render();

    }

    @Autowired
    private CmsContentService service;
    @Autowired
    private LogOperateService logOperateService;
    @Autowired
    private CmsTagService tagService;
}